using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Content;
using Scurvy.Media.VideoModel;

namespace Scurvy.Media
{
    public sealed class VideoContentManager : ContentManager
    {
        private List<TrickyStream> _streamList = new List<TrickyStream>();

        public VideoContentManager(IServiceProvider serviceProvider, string contentRoot)
            : base(serviceProvider, contentRoot)
        {
        }

        public VideoContentManager(IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
        }

        protected override System.IO.Stream OpenStream(string assetName)
        {
            TrickyStream s = new TrickyStream(base.OpenStream(assetName));
            _streamList.Add(s);
            return s;
        }

        public override T Load<T>(string assetName)
        {
            if (!(typeof(T) == typeof(Video)))
            {
                throw new ArgumentOutOfRangeException("This content manager only supports loading Videos");
            }

            return base.Load<T>(assetName);
        }

        public override void Unload()
        {
            for (int i = 0; i < _streamList.Count; i++)
            {
                _streamList[i].CloseForReal();
            }
            base.Unload();
        }
    }
}
